//
// Created by casdon on 2024/8/1.
//

#ifndef CIOT_TYPES_H
#define CIOT_TYPES_H

/**
 * Required STD lib
 */
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdint.h>
#include <time.h>

#include <ciot/config.h>

#ifdef __cplusplus
extern "C" {
#endif

#if !CIOT_SDK_FOR_ESP32 && !CIOT_SDK_FOR_IOS && !USE_INNER_CJSON
#include <cjson/cJSON.h>
#else
#include "cJSON.h"
#endif

/**
 * 由CIOT调用
 * 由适配层实现的方法或属性
 * CRUCIAL表示为必须实现的关键部分
 */
#define CIOT_ADAPTER

/**
 * 由适配层实现并主动调用的方法
 * 用于将外部状态改变发送至CIOT
 */
#define CIOT_EVENT

/**
 * 由CIOT产生，适配层或应用层接入，用于捕获相应的CIOT事件通知
 */
#define CIOT_NOTIFY

/**
 * 由CIOT实现的外部调用接口
 */
#define CIOT_API

/**
 * 由CIOT实现的内部调用接口
 */
#define CIOT_PRIVATE

CIOT_ADAPTER
CIOT_EVENT
CIOT_NOTIFY
CIOT_API
CIOT_PRIVATE

#define MAX_WIFI_SSID_LEN 32
#define MAX_WIFI_PASS_LEN 64

#define MAX_CLOUD_DATA_COUNT_PER_MSG 32

#ifndef MAX_LOG_BUFFER_LEN
#if CIOT_SDK_FOR_HOST
#define MAX_LOG_BUFFER_LEN 1024
#else
#define MAX_LOG_BUFFER_LEN 256
#endif
#endif

typedef enum Ciot_res_e {
    Ciot_OK = 0,         // 成功
    Ciot_Err = 1,        // 失败
    Ciot_Locking = 2,    // 锁定
    Ciot_Pending = 3,    // 等待
    Ciot_Fatal ,         // 严重问题
    Ciot_res_MAX
} Ciot_res;              // Ciot 基础返回状态


typedef enum Ciot_protocol_debug_level_e
{
    Ciot_protocol_debug_none = 0,
    Ciot_protocol_debug_compressed,
    Ciot_protocol_debug_development
} Ciot_protocol_debug_level;

typedef enum Ciot_product_data_type_e {
    Ciot_product_data_type_invalid = 0,
    Ciot_product_data_type_bool,
    Ciot_product_data_type_int,
    Ciot_product_data_type_double,
    Ciot_product_data_type_enum,
    Ciot_product_data_type_bits,
    Ciot_product_data_type_string,
    Ciot_product_data_type_raw,

    Ciot_product_data_type_MAX_invalid
} Ciot_product_data_type;

typedef enum Ciot_task_event_e
{
    Ciot_task_root = 0,

    Ciot_task_event_init,
    Ciot_task_event_ble_send,
    Ciot_task_event_ap_send,
    Ciot_task_event_cloud_data_send,

    Ciot_task_event_MAX
} Ciot_task_event;

#ifndef CIOT_SDK_FOR_WINDOWS

#ifndef CIOT_SDK_FOR_JAVA
#ifdef CIOT_SDK_FOR_WASM
typedef unsigned long long u_int64_t ;
typedef unsigned int u_int32_t ;
typedef unsigned short u_int16_t ;
typedef unsigned char u_int8_t  ;
#endif
#endif
#ifndef __MUSL__
typedef u_int64_t uint64_t;
typedef u_int32_t  uint32_t;
typedef u_int16_t  uint16_t;
typedef u_int8_t  uint8_t;
#endif
#endif

typedef uint64_t Ciot_time_ms;
typedef int64_t  Ciot_countdown_ms;
typedef uint64_t Ciot_time_s;
typedef uint16_t Ciot_duration_ms;


typedef bool        ciot_bool;
typedef int64_t     ciot_int;
typedef double      ciot_double;
typedef char *      ciot_string;
typedef uint8_t *   ciot_raw;

typedef struct ciot_opt_bool_s {
    ciot_bool value;
    ciot_bool is_set;
} ciot_opt_bool;

typedef struct ciot_int_optional_s {
    ciot_int value;
    ciot_bool is_set;
} ciot_opt_int;

typedef struct ciot_opt_double_s {
    ciot_double value;
    ciot_bool is_set;
} ciot_opt_double;

typedef struct ciot_opt_string_s {
    ciot_string value;
    ciot_bool is_set;
} ciot_opt_string;

typedef struct ciot_opt_raw_s {
    ciot_raw value;
    ciot_bool is_set;
} ciot_opt_raw;

const char * get_ciot_res_str( Ciot_res res );

#define CiotResStr(res) get_ciot_res_str(res)

#ifdef __cplusplus
}
#endif

#endif //CIOT_TYPES_H
